package org.jeecg.modules.medical.mapap.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.medical.areaManage.entity.Area;
import org.jeecg.modules.medical.areaManage.service.IAreaService;
import org.jeecg.modules.medical.mapap.api.vo.Result;
import org.jeecg.modules.medical.mapap.entity.MapAp;
import org.jeecg.modules.medical.mapap.entity.MapArea;
import org.jeecg.modules.medical.mapap.service.IMapApService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * AP位置信息表 前端控制器
 * </p>
 *
 * @author myName
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/map-ap")
public class MapApController {

    @Autowired
    private IMapApService mapApService;

    @Autowired
    private IAreaService areaService;

    //分页条件查询
    @GetMapping("/page")
    public Result<?> page(Integer current,Integer pageSize,MapAp mapAp){
        Page<MapAp> page = new Page<>(current, pageSize);
        QueryWrapper<MapAp> queryWrapper = new QueryWrapper<>();
        if(mapAp!=null){
            if (!StringUtils.isEmpty(mapAp.getName())){
                queryWrapper.like("name",mapAp.getName());
            }
            if (!StringUtils.isEmpty(mapAp.getMac())){
                queryWrapper.eq("mac",mapAp.getMac());
            }
            if (!StringUtils.isEmpty(mapAp.getAreaName())){
                queryWrapper.like("area_name",mapAp.getAreaName());
            }
            if (!StringUtils.isEmpty(mapAp.getDepartmentName())){
                queryWrapper.like("department_name",mapAp.getDepartmentName());
            }
        }
        mapApService.page(page, queryWrapper);
        return Result.ok(page);
    }

    //添加ap
    @PostMapping("/add")
    public Result<?> add(@RequestBody MapAp mapAp){
        boolean save = mapApService.save(mapAp);
        if (save){
            return Result.ok("ok");
        }
        return Result.error("err");
    }
    //更新ap
    @PostMapping("/update")
    public Result<?> update(@RequestBody MapAp mapAp){
        boolean save = mapApService.updateById(mapAp);
        if (save){
            return Result.ok("ok");
        }
        return Result.error("err");
    }
    //删除ap
    @PostMapping("/delBatch")
    public Result<?> delBatch(@RequestBody List<String> ids){
        boolean save = mapApService.removeByIds(ids);
        if (save){
            return Result.ok("ok");
        }
        return Result.error("err");
    }


    @GetMapping("/queryAp")
    public Result<?> queryAp(String id){
        MapAp mapAp = mapApService.getById(id);
        return Result.ok(mapAp);
    }

    @GetMapping("/areaTree")
    public org.jeecg.common.api.vo.Result<?> areaTree() {
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", "");
        List<Area> list = areaService.list(queryWrapper);
        ArrayList<MapArea> mapAreas = new ArrayList<>();

        for (Area area : list) {
            MapArea mapArea = new MapArea(area);
            List<MapArea> children = getChildren(mapArea.getId());
            mapArea.setChildren(children);
            mapAreas.add(mapArea);
        }
        return org.jeecg.common.api.vo.Result.OK(mapAreas);
    }

    public List<MapArea> getChildren(String id) {
        ArrayList<MapArea> mapAreas = new ArrayList<>();

        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        List<Area> areas = areaService.list(wrapper);
        for (Area area : areas) {
            MapArea mapArea = new MapArea(area);
            List<MapArea> children = getChildren(mapArea.getId());
            mapArea.setChildren(children);
            mapAreas.add(mapArea);
        }
        return mapAreas;
    }

}

